<?php

/**
 * This is the model class for table "payment_request".
 *
 * The followings are the available columns in table 'payment_request':
 * @property string $payment_request_id
 * @property string $owner_id
 * @property double $amount
 * @property integer $status
 * @property string $adddate
 */
class PaymentRequest extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return PaymentRequest the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'payment_request';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('owner_id, status, adddate', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('amount', 'numerical'),
			array('owner_id', 'length', 'max'=>30),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('payment_request_id, owner_id, amount, status, adddate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'payment_request_id' => 'Payment Request',
			'owner_id' => 'Owner',
			'amount' => 'Amount',
			'status' => 'Status',
			'adddate' => 'Adddate',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('payment_request_id',$this->payment_request_id,true);
		$criteria->compare('owner_id',$this->owner_id,true);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('status',$this->status);
		$criteria->compare('adddate',$this->adddate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public static function hasSent($owner_id) {
		if (self::model()->count('owner_id=:owner_id AND status=0', array(':owner_id' => $owner_id))) {
			return true;
		} else {
			return false;
		}
	}
	
	public static function getUnpaidNum() {
		return self::model()->count('status=0');
	}
}